package demo;

import com.zwz.constant.Constant;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.bcel.Const;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.Map;


@Slf4j
public class TungeeLogin {


    public  static  void main(String[] args) {
        String url = "https://user.tungee.com/api/individual-user/login";
        String boundary = "----WebKitFormBoundaryBkf3dWGARK2BBDOC"; // 与 Header 中的 boundary 一致

        // 构建 multipart/form-data 请求体
        String requestBody = "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"username\"\r\n\r\n" +
                "15366088401\r\n" +
                "--" + boundary + "\r\n" +
                "Content-Disposition: form-data; name=\"password\"\r\n\r\n" +
                "Zwz123456\r\n" +
                "--" + boundary + "--\r\n";

        try {
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.POST)
                    .header("Content-Type", "multipart/form-data; boundary=" + boundary)
                    .header("authority", "user.tungee.com")
                    .header("path", "/api/individual-user/login")
                    .header("scheme", "https")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("origin", "https://user.tungee.com")
                    .header("pragma", "no-cache")
                    .header("referer", "https://user.tungee.com/users/sign-in")
                    .header("sec-ch-ua", "\"Not_A Brand\";v=\"99\", \"Google Chrome\";v=\"109\", \"Chromium\";v=\"109\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36")
                    .header("x-requested-with", "XMLHttpRequest")
                    // 添加完整的 Cookie（从浏览器复制）
//                    .header("cookie", "__wpkreporterwid_=bdebfec7-b3b6-4533-a4f5-33ecaecc4b37; __last_enter_version=sales; Hm_lvt_d3ee3af8af62f47558a292277356e90f=1744947754,1745221273; Qs_lvt_309705=1744948492%2C1744956213%2C1744956391%2C1744956490%2C1745221273; Qs_pv_309705=3499943857613400000%2C2401862267627224600%2C2373097984748560400%2C635693979573886800%2C1483374110121918000; _uetsid=00dcdb601e8411f08e8985a5cb4ab811; _uetvid=2a1706601c0711f0be3333b35f4687c7; _co_i=6805a5576eceb9637696f09d; Hm_lvt_91b76eb18239b56f562bec1dd1b54977=1745221661,1745225090,1745227145,1745228062; HMACCOUNT=676A9C43EBB88158; acw_tc=1a0c380f17452297525887092e008411e3d8129699a45df3eb1786aafb79c2; SecurityCenterDuId=ImFBV2xWMjdPdVdOMmx2Q2hGMFNTWTVFPSI.GuepnQ.U1fIacGKKChjJvArYuXvJfJZJ1c; Hm_lpvt_91b76eb18239b56f562bec1dd1b54977=1745230255; qrcode_id=8310a793-b07a-482a-8094-368008769903; accountCenterSessionId=.eJwdjsFqwzAQRP9F5xzWsrRe5V56qRICDia5mJV2ldY1zsEmbSj994pehmF4D-bHLKoyrvzQcbuPksy-8LzqzowfYvYGc3IdI6OCkLa2lAC-BjE2oYSMiQBaQPIWXN2dpVIYclur184GZnStzdKh9wpV4CyUPCZgn5VUqZPAAp4ySerUcsGUKu9EqUGzM-v_kdif_eH1-hn72xan2_flCRCnk30bXppDn7fjcG6O_TxfhtPzOkVXxfzOy6Jzlb80md8_m7hHMg.GuerOA.YpQ3hPIAOuSqlz4_lZVbV3plaeQ")
                    .header("cookie", Constant.cookies)
                    .requestBody(requestBody)
                    .ignoreContentType(true) // 允许处理非 HTML 响应
                    .ignoreHttpErrors(true) // 避免直接抛异常
                    .execute();



           log.info("HTTP状态码: " + response.statusCode());
           log.info("响应内容: " + response.body());



            // 获取登录后的 cookies
            Map<String, String> cookies = response.cookies();

            // 步骤 3：使用 cookies 访问目标页面
            String targetUrl = "https://sales.tungee.com/unlock-list/enterprise ";
            Document doc = Jsoup.connect(targetUrl)
                    .cookies(cookies) // 使用登录后的 cookies 维持会话
                    .get();

            // 步骤 4：解析 HTML 内容
            // 示例：提取所有 <h1> 标签
            doc.select("h1").forEach(element ->log.info(element.text()));

//            // 获取登录后的 cookies
//            Map<String, String> cookies = response.cookies();
//
//            // 步骤 3：使用 cookies 访问目标页面
//            String targetUrl = "https://sales.tungee.com/home?entry=1";
//            Document doc = Jsoup.connect(targetUrl)
//                    .cookies(cookies) // 使用登录后的 cookies 维持会话
//                    .get();
//
//            // 步骤 4：解析 HTML 内容
//            // 示例：提取所有 <h1> 标签
//            doc.select("h1").forEach(element ->log.info(element.text()));


        } catch (IOException e) {
            e.printStackTrace();
        }

    }


//    public static void main(String[] args) {
//
//
//    }
}